home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / ixnet / res_init.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  6KB  |  194 lines

  1. /*-
  2.  * Copyright (c) 1985, 1989 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)res_init.c    6.15 (Berkeley) 2/24/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/param.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <arpa/nameser.h>
  43. #include <resolv.h>
  44. #include <unistd.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48.  
  49. /*
  50.  * Resolver state default settings
  51.  */
  52.  
  53. struct state _res = {
  54.     RES_TIMEOUT,                   /* retransmition time interval */
  55.     4,                             /* number of times to retransmit */
  56.     RES_DEFAULT,            /* options flags */
  57.     1,                             /* number of name servers */
  58. };
  59.  
  60. /*
  61.  * Set up default settings.  If the configuration file exist, the values
  62.  * there will have precedence.  Otherwise, the server address is set to
  63.  * INADDR_ANY and the default domain name comes from the gethostname().
  64.  *
  65.  * The configuration file should only be used if you want to redefine your
  66.  * domain or run without a server on your machine.
  67.  *
  68.  * Return 0 if completes successfully, -1 on error
  69.  */
  70. int
  71. res_init(void)
  72. {
  73.     register FILE *fp;
  74.     register char *cp, **pp;
  75.     register int n;
  76.     char buf[BUFSIZ];
  77.     int nserv = 0;    /* number of nameserver records read from file */
  78.     int haveenv = 0;
  79.     int havesearch = 0;
  80.  
  81.     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  82.     _res.nsaddr.sin_family = AF_INET;
  83.     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  84.     _res.nscount = 1;
  85.  
  86.     /* Allow user to override the local domain definition */
  87.     if ((cp = getenv("LOCALDOMAIN")) != NULL) {
  88.         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  89.         haveenv++;
  90.     }
  91.  
  92.     if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
  93.         /* read the config file */
  94.         while (fgets(buf, sizeof(buf), fp) != NULL) {
  95.         /* read default domain name */
  96.         if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
  97.             if (haveenv)    /* skip if have from environ */
  98.                 continue;
  99.             cp = buf + sizeof("domain") - 1;
  100.             while (*cp == ' ' || *cp == '\t')
  101.                 cp++;
  102.             if ((*cp == '\0') || (*cp == '\n'))
  103.                 continue;
  104.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  105.             if ((cp = index(_res.defdname, '\n')) != NULL)
  106.                 *cp = '\0';
  107.             havesearch = 0;
  108.             continue;
  109.         }
  110.         /* set search list */
  111.         if (!strncmp(buf, "search", sizeof("search") - 1)) {
  112.             if (haveenv)    /* skip if have from environ */
  113.                 continue;
  114.             cp = buf + sizeof("search") - 1;
  115.             while (*cp == ' ' || *cp == '\t')
  116.                 cp++;
  117.             if ((*cp == '\0') || (*cp == '\n'))
  118.                 continue;
  119.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  120.             if ((cp = index(_res.defdname, '\n')) != NULL)
  121.                 *cp = '\0';
  122.             /*
  123.              * Set search list to be blank-separated strings
  124.              * on rest of line.
  125.              */
  126.             cp = _res.defdname;
  127.             pp = _res.dnsrch;
  128.             *pp++ = cp;
  129.             for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
  130.                 if (*cp == ' ' || *cp == '\t') {
  131.                     *cp = 0;
  132.                     n = 1;
  133.                 } else if (n) {
  134.                     *pp++ = cp;
  135.                     n = 0;
  136.                 }
  137.             }
  138.             /* null terminate last domain if there are excess */
  139.             while (*cp != '\0' && *cp != ' ' && *cp != '\t')
  140.                 cp++;
  141.             *cp = '\0';
  142.             *pp++ = 0;
  143.             havesearch = 1;
  144.             continue;
  145.         }
  146.         /* read nameservers to query */
  147.         if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
  148.            nserv < MAXNS) {
  149.             cp = buf + sizeof("nameserver") - 1;
  150.             while (*cp == ' ' || *cp == '\t')
  151.                 cp++;
  152.             if ((*cp == '\0') || (*cp == '\n'))
  153.                 continue;
  154.             if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
  155.             inet_addr(cp)) == (unsigned)-1) {
  156.                 _res.nsaddr_list[nserv].sin_addr.s_addr
  157.                 = INADDR_ANY;
  158.                 continue;
  159.             }
  160.             _res.nsaddr_list[nserv].sin_family = AF_INET;
  161.             _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
  162.             nserv++;
  163.             continue;
  164.         }
  165.         }
  166.         if (nserv > 1) 
  167.         _res.nscount = nserv;
  168.         (void) fclose(fp);
  169.     }
  170.     if (_res.defdname[0] == 0) {
  171.         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
  172.            (cp = index(buf, '.')))
  173.             (void)strcpy(_res.defdname, cp + 1);
  174.     }
  175.  
  176.     /* find components of local domain that might be searched */
  177.     if (havesearch == 0) {
  178.         pp = _res.dnsrch;
  179.         *pp++ = _res.defdname;
  180.         for (cp = _res.defdname, n = 0; *cp; cp++)
  181.             if (*cp == '.')
  182.                 n++;
  183.         cp = _res.defdname;
  184.         for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH;
  185.             n--) {
  186.             cp = index(cp, '.');
  187.             *pp++ = ++cp;
  188.         }
  189.         *pp++ = 0;
  190.     }
  191.     _res.options |= RES_INIT;
  192.     return (0);
  193. }
  194.